package org.example.journal_items.UserDao;

import org.example.journal_items.Entity.Todo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;

import java.util.Date;
import java.util.List;

@Mapper
public interface TodoDao {
  // 获取用户的所有待办事项
  @Select("SELECT id, user_id as userId, title, description, due_date as dueDate, is_completed as isCompleted, created_at as createdAt, updated_at as updatedAt FROM todos WHERE user_id = #{userId} ORDER BY due_date ASC, created_at DESC")
  List<Todo> findByUserId(@Param("userId") Long userId);

  // 获取用户指定日期的待办事项
  @Select("SELECT id, user_id as userId, title, description, due_date as dueDate, is_completed as isCompleted, created_at as createdAt, updated_at as updatedAt FROM todos WHERE user_id = #{userId} AND due_date = #{dueDate} ORDER BY created_at DESC")
  List<Todo> findByUserIdAndDueDate(@Param("userId") Long userId, @Param("dueDate") String dueDate);

  // 根据ID获取待办事项
  @Select("SELECT id, user_id as userId, title, description, due_date as dueDate, is_completed as isCompleted, created_at as createdAt, updated_at as updatedAt FROM todos WHERE id = #{id}")
  Todo findById(@Param("id") Long id);

  // 创建待办事项
  @Insert("INSERT INTO todos(user_id, title, description, due_date, is_completed, created_at, updated_at) VALUES(#{userId}, #{title}, #{description}, #{dueDate}, #{isCompleted}, NOW(), NOW())")
  void insertTodo(Todo todo);

  // 更新待办事项
  @Update("UPDATE todos SET title = #{title}, description = #{description}, due_date = #{dueDate}, is_completed = #{isCompleted}, updated_at = NOW() WHERE id = #{id}")
  void updateTodo(Todo todo);

  // 删除待办事项
  @Delete("DELETE FROM todos WHERE id = #{id}")
  void deleteTodo(@Param("id") Long id);

  // 切换待办事项完成状态
  @Update("UPDATE todos SET is_completed = #{isCompleted}, updated_at = NOW() WHERE id = #{id}")
  void toggleTodoStatus(@Param("id") Long id, @Param("isCompleted") Boolean isCompleted);
}